/**
 * 阿里云语音识别-一句话识别 restful Api
 * 
 * @param {filePath} 音频临时oss地址
 * @param {appkey} 服务端返回的阿里云中的appkey
 * @param {token} 服务端返回的阿里云中的token
 * @return {string|boolean} 成功返回文件地址，失败返回false
 */
const audio_util = {
	audioToText(option = {
		token,
		filePath,
		appkey,
		format
	}) {
		let url = 'https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/asr';
		let sampleRate = '16000';
		let enablePunctuationPrediction = true;
		let enableInverseTextNormalization = true;
		let enableVoiceDetection = false;
		/**
		 * 设置RESTful请求参数
		 */
		let requestUrl = url;
		requestUrl = requestUrl + '?appkey=' + option.appkey;
		requestUrl = requestUrl + '&format=' + option.format;
		requestUrl = requestUrl + '&sample_rate=' + sampleRate;
		if (enablePunctuationPrediction) {
			requestUrl = requestUrl + '&enable_punctuation_prediction=' + 'true';
		}
		if (enableInverseTextNormalization) {
			requestUrl = requestUrl + '&enable_inverse_text_normalization=' + 'true';
		}
		if (enableVoiceDetection) {
			requestUrl = requestUrl + '&enable_voice_detection=' + 'true';
		}
		requestUrl = requestUrl + '&audio_address=' + option.filePath;
		uni.showLoading({
			title: '处理中',
			mask: true
		});
		uni.request({
			url: requestUrl,
			method: 'POST',
			header: {
				'X-NLS-Token': option.token,
				'content-type': 'application/octet-stream',
			},
			data: {},
			success: (res) => {
				if (res.data.status == 20000000) {
					console.log(res.data);
				} else {
					console.log(res);
				}
			},
			fail: (res) => {
				console.log(res);
			},
			complete: () => {
				uni.hideLoading();
			}
		})
	}
}
module.exports = audio_util